<template>
	<view class="content">
		<view class="log-head">
			<view class="up">
				余额变更记录
			</view>
		</view>
		<view class="title">
			<view>明细</view>
			<view>当月收益：{{(Math.floor(money*100)/100)}}</view>
			<picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindDateChange" fields='month' >
				<view class="uni-input">{{date}}▼</view>
			</picker>
		</view>
		<view class="list">
			<view class="info" v-for="(item,index1) in list" :key='index1' style="display: flex;align-items: center;">
				<view style="width: 50%;">
					<view>
						{{item.create_time}}
					</view>
				</view>

				<view style="width: 25%;text-align: center;">
					{{item.source}}
				</view>

				<view class="money" style="width: 25%;text-align: center;">
					+{{item.day_profit}}
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				list: [],
				date: currentDate,
				money:0.00,
				// clickmo:true
			};
		},
		onLoad(option) {
			this.info(option.time)
			// calculateTotalPrice(list)
		},
		computed:{
			startDate(){
				return this.getDate('start');
			},
			endDate(){
				return this.getDate('end');
			}
		},
		// filters:{
		// 	sum(item){
		// 		let sum = 0
		// 		item.forEach(item1 => {
		// 			sum += item1.barcodeQty
		// 		})
		// 		return sum
		// 		console.log(sum)
		// 	}

		// },
		methods: {
			bindDateChange: function(e) {
				this.date = e.detail.value
				this.money = 0
				this.info()
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				
				if (type === 'start') {
					year = year - 1;
				} else if (type === 'end') {
					year = year + 10;
				}
				month = month > 9 ? month : '0' + month;
				
				return `${year}-${month}`;
			},
			// calculateTotalPrice(list) {
			//     // 使用reduce方法进行累加操作
			//     const total = list.reduce((sum, a) => {
			//         // 只累加价格大于0的商品
			//         if (a.day_profit > 0) {
			//             return sum + a.day_profit;
			//         }
			//         return sum;
			//     }, 0); // 初始值为0
			
			//     return total;
			// 	console.log(calculateTotalPrice(productList));
			// },
			info(time) {
				let userinfo = uni.getStorageSync('userinfo');
				uni.showLoading({
					title: "加载中"
				})
				this.$api.interfaceApi('getBalanceLogByDate')({
					id: userinfo.id,
					date:this.date
				}).then(res => {
					if (res.code == 200) {
						this.list = res.data
						// if(this.clickmo == true){
						// 	this.money = 0
						// 	console.log(this.clickmo,this.money)
						// }
						for(var j = 0;j<res.data.length;j++){
							this.money +=res.data[j].day_profit*1
							// console.log(this.money);
						}
						
						console.log(this.money);
					}
					uni.hideLoading()
				})
			}

		}
	};
</script>

<style lang="less" scoped>
	.content {

		.log-head {
			background-color: #028bbc;
			color: #FFFFFF;

			.up {
				height: 100rpx;
				line-height: 100rpx;
				text-align: center;
			}
		}

		.title{
			background-color: #FFFFFF;
			margin-top: 20rpx;
			color: #666666;
			display: flex;
			justify-content: space-between;
			padding: 20rpx 20rpx;
		}
		
		.list {
			background-color: #FFFFFF;

			.title {
				padding: 20rpx 20rpx;
			}

			.info {
				display: flex;
				justify-content: space-between;
				padding: 20rpx;
				border-top: 1rpx solid #e2e2e2;

				.con {
					//line-height: 90rpx;
				}

				.money {
					//line-height: 90rpx;
					color: #ff0000;
				}
			}
		}


	}
</style>